package com.yejiali.backend.handler;

import com.alibaba.fastjson.JSON;
import com.yejiali.backend.common.httpcode.HttpCodeEnum;
import com.yejiali.backend.response.Response;
import com.yejiali.backend.utils.WebUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author:yejiali
 * @Date:2025/5/11-05-11-15:42
 * @Decsription:com.yejiali.backend.handler
 * @Version:1.0
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        accessDeniedException.printStackTrace();
        Response result = Response.error(HttpCodeEnum.NO_OPERATOR_AUTH.getCode(),"权限不足");
        //响应给前端
        WebUtil.renderString(response, JSON.toJSONString(result));
    }
}
